package br.ufpi.repositories.Implement;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.com.caelum.vraptor.ioc.Component;
import br.ufpi.model.User;
import br.ufpi.repositories.Repository;
import br.ufpi.repositories.UserRepository;

@Component
public class UserRepositoryImpl extends Repository<User, Long> implements UserRepository {

	public UserRepositoryImpl(EntityManager entityManager) {
		super(entityManager);
	}

	@Override
	public User logar(String login, String password) {
		Query query = entityManager.createNamedQuery("User.login");
		query.setParameter("login", login);
		query.setParameter("passWord", password);
		try {

			return (User) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@Override
	public User findConfirmationEmail(String confirmationEmail) {
		Query createNativeQuery = entityManager.createNamedQuery("User.findByConfirmationEmail");
		createNativeQuery.setParameter("confirmationEmail", confirmationEmail);
		try {

			return (User) createNativeQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@Override
	public User findEmail(String email) {
		Query createNamQuery = entityManager.createNamedQuery("User.findByEmail");
		createNamQuery.setParameter("email", email);
		try {
			return (User) createNamQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@Override
	public boolean isContainsEmail(String email) {
		return findEmail(email) == null ? false : true;
	}

	@Override
	public User load(User user) {
		Query createNativeQuery = entityManager.createNamedQuery("User.findById");
		createNativeQuery.setParameter("id", user.getId());
		try {
			return (User) createNativeQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@Override
	public User findNickName(String nickName) {
		Query createNamQuery = entityManager.createNamedQuery("User.findByNickName");
		createNamQuery.setParameter("nickName", nickName);
		try {
			return (User) createNamQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@Override
	public boolean isContainsLogin(String login) {
		return this.findNickName(login) == null ? false : true;
	}

	@Override
	public User findLogin(String login) {
		Query createNamQuery = entityManager.createNamedQuery("User.findByLogin");
		createNamQuery.setParameter("login", login);
		try {
			return (User) createNamQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	@Override
	public boolean isContainTokenActive(String confirmacaoEmail) {
		return this.findTokenActive(confirmacaoEmail) == null ? false
				: true;
	}
	@Override
	public User findTokenActive(String tokenActive) {
		Query createNativeQuery = entityManager
				.createNamedQuery("User.findTokenActive");
		createNativeQuery.setParameter("tokenActive", tokenActive);
		try {

			return (User) createNativeQuery.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

}
